import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd';
import { InitDataPipe } from 'src/app/base/pipe/initdata.pipe';

declare var ui: any;
declare var form: any;
declare var security: any;

@Component({
  templateUrl: './passenger.check.info.component.html'
})
export class PassengerCheckInfoComponent implements OnInit {

  ss: any;
  // 当前数据
  fs: any;
  // 菜单
  menu = null;
  // 字典
  airportDic: any = null;
  constructor(
    public route: ActivatedRoute,
    private modalService: NzModalService,
    private initdataPipe: InitDataPipe) {
    this.ss = security.SecurityService;
    this.ss.show = false;
    this.menu = new ui.TabService('bpm', 's');
    this.menu.setTab('main');
    // 注册表单
    this.fs = new form.FormSimpleService('cyclepassengercheck', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id']
    });

    this.airportDic = new form.DictionaryService('zd_airport');
    this.airportDic.loadAll();
  }

  ngOnInit() {
    this.initData();

    // 浏览器关闭监听
    window.onbeforeunload = () => {
      // tslint:disable-next-line: deprecation
      const e: any = window.event;
      e.returnValue = ('请确认已保存，是否离开？');
      return e;
    };
  }

  initData() {
    let id = '-1';
    this.route.queryParams.subscribe((res) => {
      if (res.id) {
        id = res.id;
      }
    });
    this.fs.editRowByKey(id, (data: any) => {
      if (this.fs.er.airport === undefined) {
        this.fs.er.airport =  this.ss.user.userDepCode[0];
      }
      if (data.FIELD_ISNEW) {
        this.initdataPipe.transform(this.fs.er, 'cycle-passenger');
      }
    });
  }

  save() {
    if (!this.fs.er.airport) {
      this.modalService.error({
        nzTitle: '保存失败',
        nzContent: '未填写所属机场！'
      });
      return ;
    }
    this.fs.save(this.fs.er, (data) => {
      this.modalService.success({
        nzTitle: '系统提示',
        nzContent: '保存成功！'
      });
      this.fs.er = data;
    }, (error) => {
      this.modalService.error({
        nzTitle: '系统提示',
        nzContent: '保存失败！'
      });
    });
  }

  close() {
    window.close();
  }
}
